package com.example.zju_bytedance_android_hw1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;


public class Function extends AppCompatActivity implements View.OnClickListener{

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.item_return:
                finish();
                break;
        }

        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function);
        Intent intent=getIntent();
        String name=intent.getStringExtra("inName");
        if(name.isEmpty())
        {
            name="traveller";
        }
        this.setTitle("Welcome,"+name+"!");
        image=(ImageView)findViewById(R.id.showImage);
        bar=(ProgressBar)findViewById(R.id.progress_bar);
    }

    private ImageView image;
    private ProgressBar bar;
    public void onClick(View view) {

        boolean checked = ((RadioButton) view).isChecked();
        switch(view.getId()) {
            case R.id.radio_number_one:
                if (checked){
                    AlertDialog.Builder dialog=new AlertDialog.Builder(Function.this);
                    dialog.setTitle("Zhejiang University");
                    dialog.setMessage("You choose Zhejiang University");
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            bar.setVisibility(View.VISIBLE);

                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    Log.d("first", "run: Choose ZJU and delay");
                                    bar.setVisibility(View.INVISIBLE);
                                    image.setImageResource(R.drawable.zju);
                                }
                            }, 300);//3秒后执行Runnable中的run方法

                        }
                    });
                    dialog.show();

                }

                    break;
            case R.id.radio_number_two:
                if (checked){
                    AlertDialog.Builder dialog=new AlertDialog.Builder(Function.this);
                    dialog.setTitle("Bytedance");
                    dialog.setMessage("You choose Bytedance");
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            bar.setVisibility(View.VISIBLE);

                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    Log.d("second", "run: Choose Byte and delay");
                                    bar.setVisibility(View.INVISIBLE);
                                    image.setImageResource(R.drawable.bytedance);
                                }
                            }, 300);//3秒后执行Runnable中的run方法
                        }
                    });
                    dialog.show();

                }

                    break;
        }
    }
}
